package yja.gzw.app.chengjiafang

import android.content.Intent
import android.net.Uri
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import cn.jzvd.Jzvd
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.tencent.imsdk.v2.V2TIMConversation
import com.tencent.qcloud.tuicore.TUIConstants
import com.tencent.qcloud.tuikit.tuichat.classicui.page.TUIC2CChatActivity
import yja.gzw.app.R
import yja.gzw.app.activity.ItJvjueActivity
import yja.gzw.app.activity.QufabuActivity
import yja.gzw.app.activity.ShipingActivity
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityChengjiedaishouhuoBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter

class ChengjiedaishouhuoActivity :BaseActivity<Mainviewmodel, ActivityChengjiedaishouhuoBinding>(R.layout.activity_chengjiedaishouhuo){
    var userid=""
    var qian=""
    var types=""
    var leixing="wechat"
    override fun initView() {
        val ids =intent.getStringExtra("ids")

        binding.sixing.setOnClickListener {
            Log.e("userid2","userid2是"+userid)
            val intent = Intent(this, TUIC2CChatActivity::class.java)
            intent.putExtra(TUIConstants.TUIChat.CHAT_ID,userid)
            intent.putExtra(TUIConstants.TUIChat.CHAT_TYPE, V2TIMConversation.V2TIM_C2C)
            startActivity(intent)


        }

        viewmodel.onxiangqingwang(UserData.token, mapOf("order_id" to ids!!))
        binding.quxiao.setOnClickListener {
//            finish()
//            val inten= Intent(this, ItJvjueActivity::class.java)
//            inten.putExtra("oids",ids)
//            startActivity(inten)
            viewmodel.onchengjiefangquxiaodingdanwang(UserData.token, mapOf("order_id" to ids))

        }
        binding.xiugai.setOnClickListener {
            val inten=Intent(this, ShipingActivity::class.java)
            inten.putExtra("id",ids)
            startActivity(inten)



        }






    }

    override fun dataObservese() {
        viewmodel.onchengjiefangquxiaodingdan.observe(this, Observer {
            if(it.code==1){
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
                finish()
            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }

        })


        viewmodel.onxiangqing.observe(this, Observer {
            if(it.code==1){

                if(it.data.fine_release_type==1){
                    binding.fabumoshioqian.text="自己定价"
                }else if(it.data.fine_release_type==2){
                    binding.fabumoshioqian.text="第二方定价"
                }else{
                    binding.fabumoshioqian.text="第二方竞价"
                }
                binding.platformId.text=it.data.platform_id
                binding.weizhi.text=it.data.address
                binding.neirong.text=it.data.content
                binding.huobiaoqian.text=it.data.need_title
                binding.wengzisucaishow.text= it.data.label
                binding.biaotifu.text=it.data.title


                Log.e("承接走到了","走到了")
                binding. videoView.setUp(it.data.video_url.toString(), "播放视频", Jzvd.SCREEN_WINDOW_NORMAL)
                binding. videoView.thumbImageView.setImageURI(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"));
                types=it.data.type
                //todo 承接方的用户信息
                userid=it.data.publisher.id.toString()

                Log.e("承接方用户id",it.data.publisher.toString())

                binding.biaoti.text=it.data.title

                if(it.data.type=="1"){
                    binding.zhuanquleixing.text="三元视频"

                }else if(it.data.type=="2"){
                    binding.zhuanquleixing.text="四元视频"
                }else{
                    binding.zhuanquleixing.text="精品视频"
                }
                binding.fabumoshioqian.text="已出价￥${it.data.pay_price}"

                binding.wengzisucaishow.text=it.data.text_material
                val requestOptions = RequestOptions.bitmapTransform(CircleCrop())
                Glide.with(this).load("http://117ai.32.hzgqapp.com/"+it.data.publisher.avatar)
                    .apply(requestOptions)
                    .into(binding.touxiang)
                binding.name.text=it.data.publisher.username
                binding.dingdanbiaohao.text=it.data.order_no
                val timestamp = 1739169752L
                // 将秒级时间戳转换为 Instant 对象
                //todo 爆红不必理会
                val instant = Instant.ofEpochSecond(timestamp)

                val zoneId = ZoneId.systemDefault()
                // 将 Instant 对象转换为 LocalDateTime 对象
                val localDateTime = instant.atZone(zoneId).toLocalDateTime()
                // 定义日期时间格式化模式
                val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                // 格式化日期时间
                val formattedDateTime = localDateTime.format(formatter)
                binding.dindanfabushijian.text=it.data.create_time_text
                binding.dindanchengjiaoshijian.text=it.data.confirm_time_text
                binding.dindanwanchengshijian.text=it.data.delivery_time_text
                binding.dindanjiage.text="￥${it.data.pay_price}"
                qian=it.data.price_list[0].price
                binding.dingjiaqianshu.text="第二方定价：￥${it.data.pay_price}"


            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }

        })
    }
}